<?php


namespace App\Admin\Extensions;


use Encore\Admin\Grid\Tools\BatchAction;

class Transfer extends BatchAction
{
    protected $action;

    public function __construct($action = 1)
    {
        $this->action = $action;
    }

    public function script()
    {
        return <<<EOT
  $('{$this->getElementClass()}').on('click', function() {
  swal({
  title: '请输入业务员的id',
  input: 'text',
  showCancelButton: true,
  confirmButtonText: '确定',
  showLoaderOnConfirm: true,
  preConfirm: function(email) {
    return new Promise(function(resolve, reject) {
       $.ajax({
                  method: 'post',
                    url: '{$this->resource}/getUser',
                    data: {
                        _token:LA.token,
                        id: email,
                    },
                    success: data=> {
                       if(data.code==1){
                            resolve()
                       }else{
                            swal('该业务员id不存在','','error') 
                            return  
                            reject('该业务员id不存在')
                       }
                    }
                });
            }).catch((e) => {
                 swal(e,'','error')   
            });
  },
  allowOutsideClick: false
}).then(function(email) {

                $.ajax({
                    method: 'post',
                    url: '{$this->resource}/transfer',
                    data: {
                        _token:LA.token,
                        ids: selectedRows(),
                        user_id:email.value,
                        action: {$this->action}
                    },
                    success: function (data) {
                          if(data.code==0){
                              $.pjax.reload('#pjax-container');
                        toastr.success('操作成功');
                          }  else{
                             toastr.error('请选择客户');
                          }
                    
                    }
                });
        
 
}),function(error){
        

}
        });      

EOT;
    }

}
